//
//  LCDetialViewController.m
//  段子手
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCDetialViewController.h"
//#import "LCCellFactory.h"

@interface LCDetialViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UILabel *labelNum;

@property (nonatomic, retain)  UIView *headView;

@property (nonatomic, retain) NSMutableArray *arrModel;


@end

@implementation LCDetialViewController


//- (void)setModel:(LCHomeTableViewPassageModel *)model{
//    
//    
//}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor orangeColor];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - 64) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [_tableView registerClass:[LCContentTableViewCell class] forCellReuseIdentifier:@"contentTableViewCell"];
    
    [self.view addSubview:_tableView];
    
    
    
    [self makeCell];
    
    [self dataHandle];
    
    

    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCContentTableViewCell *contentTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"contentTableViewCell"];
    
    LCHomeCommentModel *model = _arrModel[indexPath.row];
    
    contentTableViewCell.model = model;
    
    
    return contentTableViewCell;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _arrModel.count;
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LCHomeCommentModel *model = _arrModel[indexPath.row];

    
    NSString *textStr = model.text;
    
   CGRect rectCommentText = [WxlAPPTools scripeSuitWidthWithString:textStr Font:13 Size:CGSizeMake(SCREENW - 80, 0)];
    
//        NSLog(@"$$$$$$$%f", 50 + rectCommentText.size.height);
    
    return  50 + rectCommentText.size.height;
}




- (void)makeCell{
    

    if ([_model.CellStr isEqualToString:@"LCHomePassageTableViewCell"]) {
        
        LCHomePassageTableViewCell *cell = [[LCHomePassageTableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"passageCell"];
        
             cell.frame = CGRectMake(0, 0, SCREENW, _CellH );
        cell.model = _model;

        
        self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, _CellH + 30)];
        [_headView addSubview:cell];
        
        _tableView.tableHeaderView = _headView;
        
        
        
    }else if ([_model.CellStr isEqualToString:@"LCHomeVedioTableViewCell"]){
        
        LCHomeVedioTableViewCell *cell = [[LCHomeVedioTableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"vedioTableViewCell"];
        cell.frame = CGRectMake(0, 0, SCREENW, _CellH );

        cell.model = _model;
        
      self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, _CellH + 30)];
        [_headView addSubview:cell];

        _tableView.tableHeaderView = _headView;
        
        
    }else if ([_model.CellStr isEqualToString:@"LCHomeImageTableViewCell"]){
        LCHomeImageTableViewCell *cell = [[LCHomeImageTableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"imageCell"];
        cell.model = _model;
             cell.frame = CGRectMake(0, 0, SCREENW, _CellH );
         self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, _CellH + 30)];
        [_headView addSubview:cell];
        
        _tableView.tableHeaderView = _headView;
        
        
    }
    

    

}



- (void)dataHandle{
    
    NSString *str = [NSString stringWithFormat:@"http://isub.snssdk.com/neihan/comments/?group_id=%@&item_id=6115438030&count=20&offset=0&iid=3599529901&device_id=5493634716&ac=wifi&channel=meizu&aid=7&app_name=joke_essay&version_code=500&version_name=5.0.0&device_platform=android&ssmix=a&device_type=M353&os_api=19&os_version=4.4.4&uuid=861138023246677&openudid=13d3ef14d7878c0f&manifest_version_code=500", _model.id_str];
    
    
    [WxlAPPTools GETWithURL:str par:nil success:^(id responseObject) {
        
        self.arrModel = [LCHomeCommentModel modelHandelWithArray:responseObject[@"data"][@"top_comments"]];
        
           [_tableView reloadData];
        
        if (_arrModel.count == 0) {
            _tableView.separatorColor = [UIColor clearColor];
        }
        
        
        self.labelNum = [[UILabel alloc]initWithFrame:CGRectMake(20, _CellH, 200, 30)];
        
        _labelNum.font = [UIFont systemFontOfSize:13];
        
        _labelNum.textColor = [UIColor colorWithRed:0.99 green:0.25 blue:0.57 alpha:1];
        
//        _labelNum.backgroundColor = [UIColor yellowColor];
        _labelNum.text = [NSString stringWithFormat:@"新鲜评论（%lu）", (unsigned long)_arrModel.count];
        
        
            [_headView addSubview:_labelNum];
        
        NSLog(@"%@", _labelNum.text);
        
     
        
        
    } filed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
